<?php
/**
 * Outsource.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-19
 * Time: 13:59
 */

namespace App\Http\Controllers\Api\Customer;


use App\Http\Controllers\Base;
use App\Tables;

class Outsource extends Base
{
    public function getList()
    {
        $name           = $this->request->get('name');
        $businessLines  = $this->request->get('businessLines');

        $t = Tables\Outsource::query();
        if(!empty($name)) {
            $t->where('name', $name)->orWhere('name', 'like', "%{$name}%");
        }

        if(!empty($businessLines)) {
            $t->where('business_lines', $businessLines)->orWhere('business_lines', 'like', "%{$businessLines}%");
        }

        $list = $t->get();
        $r = [];

        foreach($list as $v) {
            $r[] = [
                'id' => $v['id'], 'name' => $v['name'], 'pyCode' => $v['py_code'], 'businessLines' => $v['business_lines'],
                'address' => $v['address'], 'qq' => $v['qq'], 'wx' => $v['wx'], 'phone' => $v['phone']
            ];
        }
        return $this->responseSuccess($r);
    }

    public function get()
    {
        $id = $this->request->get('id', 0);
        if(empty($id)) {
            return $this->responseSuccess();
        }
        $find = Tables\Outsource::find($id);

        return $this->responseSuccess( [
            'id' => $find['id'], 'name' => $find['name'], 'pyCode' => $find['py_code'], 'businessLines' => $find['business_lines'],
            'address' => $find['address'], 'qq' => $find['qq'], 'wx' => $find['wx'],'phone' => $find['phone']
        ]);
    }

    public function add()
    {
        // name:'', pyCode:'', businessLines:'',phone:'',address:'',qq:'',wx:''
        $name           = $this->request->post('name', '');
        $pyCode         = $this->request->post('pyCode');
        $businessLines  = $this->request->post('businessLines');
        $phone          = $this->request->post('phone');
        $address        = $this->request->post('address');
        $qq             = $this->request->post('qq');
        $wx             = $this->request->post('wx');

        if(empty($name)) {
            return $this->responseFail('名称不能为空！');
        }

        $ok = (bool) Tables\Outsource::insert([
            'name' => $name, 'py_code' => $pyCode, 'business_lines' => $businessLines,
            'phone' => $phone, 'address' => $address, 'qq' => $qq, 'wx' => $wx,
            'add_time' => time()
        ]);
        return $ok ? $this->responseSuccess() : $this->responseFail();
    }

    public function update()
    {
        $id             = (int) $this->request->post('id', 0);
        $name           = $this->request->post('name', '');
        $pyCode         = $this->request->post('pyCode');
        $businessLines  = $this->request->post('businessLines');
        $phone          = $this->request->post('phone');
        $address        = $this->request->post('address');
        $qq             = $this->request->post('qq');
        $wx             = $this->request->post('wx');

        if($id <= 0) {
            return $this->responseFail('无数据');
        }

        if(empty($name)) {
            return $this->responseFail('名称不能为空！');
        }

        $ok = (bool) Tables\Outsource::where('id', $id)->update([
            'name' => $name, 'py_code' => $pyCode, 'business_lines' => $businessLines,
            'phone' => $phone, 'address' => $address, 'qq' => $qq, 'wx' => $wx,
        ]);
        return $ok ? $this->responseSuccess() : $this->responseFail();
    }

    public function del()
    {
        $id = (int) $this->request->post('id',  0);
        if($id <= 0) {
            return $this->responseFail('没有数据');
        }

        $find = Tables\Outsource::find($id);
        if(empty($find)) {
            return $this->responseFail('未发现数据');
        }

        $oidList = Tables\OrderDetail::query()
            ->where('outsource_id', $id)
            ->where('make_status', '!=', Tables\Order::Make_Status_Ok)
            ->pluck('oid');
        if($oidList->isNotEmpty()) {
            $count = Tables\Order::query()
                ->whereIn('id', $oidList)
                ->where('dumped', Tables\Order::No_Dumped)
                ->count();
            if($count > 0) {
                return $this->responseFail('有未完成的外协单，无法删除！');
            }
        }


        return (bool) $find->delete() ? $this->responseSuccess() : $this->responseFail();
    }
}